import axios from 'axios'
import router from "@/router";
import { ElNotification } from 'element-plus'


axios.defaults.baseURL='http://101.43.150.171:8082/'
// axios.defaults.baseURL='http://127.0.0.1:8082/'
const request = axios.create({
	timeout: 5000,
	headers: {
		'Content-Type': "application/json; charset=utf-8"
	}
})

request.interceptors.request.use(config => {
	config.headers['Authorization'] = localStorage.getItem("token")
	return config
})


request.interceptors.response.use(
	response => {

		// console.log("response ->" ,response.data.data)

		let res = response.data

		if (res.status === 200) {
			return response
		} else if(response.status === 200){
			return response;
		}else{
			ElNotification.error('服务器连接异常')
			return Promise.reject(response.data.data.message)
		}
	},
	error => {

		// console.log('error',error)

		if (error.response.status === 400) {
			ElNotification.error("权限不足", {duration: 3000})
		}

		if (error.response.data) {
			error.massage = error.response.data.message
		}

		if (error.response.status === 401) {
			sessionStorage.clear();
			localStorage.clear();
			router.push("/login")
			location.reload();
		}

		ElNotification.error(error.massage, {duration: 3000})
		return Promise.reject(error)
	}
)

export default request